#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

//栈的初始化
void stackInit(Stack* pst)
{
	assert(pst);
	pst->data = NULL;
	pst->size = pst->capacity = 0;
}
//栈的销毁
void stackDestroy(Stack* pst)
{
	if (pst->data != NULL)
	{
		free(pst->data);
		pst->data = NULL;
	}
	pst->capacity = pst->size = 0;
}
//扩容
void checkCapacity(Stack* pst)
{
	assert(pst);
	if (pst->size == pst->capacity)
	{
		//计算增容多大
		int newCapacity = pst->size == 0 ? 4 : 2 * pst->capacity;
		//增容
		StackDataType* newData = (StackDataType*)realloc(pst->data, sizeof(StackDataType) * newCapacity);
		if (newData == NULL)
		{
			perror("realloc");
			exit(1);
		}
		pst->capacity = newCapacity;
		pst->data = newData;
	}
}
//栈是否为空
bool stackIsEmpty(Stack* pst)
{
	assert(pst);
	if (pst->size > 0)
		return false;
	return true;
}
//入栈
void stackPush(Stack* pst, StackDataType data)
{
	assert(pst);
	checkCapacity(pst);
	*(pst->data + pst->size++) = data;
}
//出栈
void stactPop(Stack* pst)
{
	assert(pst && pst->size);
	pst->size--;
}
//返回栈顶的元素
StackDataType stackTop(Stack* pst)
{
	assert(pst && pst->size);
	return *(pst->data + pst->size-1);
}
//返回栈中元素的个数
int stackSize(Stack* pst)
{
	assert(pst);
	return pst->size;
}


